﻿using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using DreamCube.Foundation.Basic.Utility;

namespace SInfoActiveX.Common
{
#if publish
    [Guid("BF593D7F-E0BE-4CED-B260-7E99DE9704A5")]
#else
    [Guid("68BC0A33-44F8-4B95-81B7-654E0EE939F2")]
#endif
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public class ClipboardHelper
    {
        /// <summary>
        /// 判断剪切板是否存在图片数据
        /// </summary>
        public Boolean IsClipboardContainsImage()
        {
            return Clipboard.ContainsImage();
        }

        /// <summary>
        /// 把剪切板的图片数据post到指定的服务器页面接收；返回图片的url
        /// </summary>
        /// <param name="url"></param>
        public String PostClipboardImageToUrl(String url)
        {
            try
            {
                String fileName = MyIO.GetRandomFileName("png");
                String fileFullName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,fileName);
                if (SaveClipboardImage(fileFullName))
                {
                    return MyWeb.PostTo(url, fileFullName, "file", null, null);
                }
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
            return String.Empty;
        }

        /// <summary>
        /// 把剪切板的图片保存到指定的目录
        /// </summary>
        /// <param name="fileName"></param>
        public Boolean SaveClipboardImage(String fileName)
        {
            try
            {
                if (Clipboard.ContainsImage())
                {
                    Image img = Clipboard.GetImage();
                    if (img != null)
                    {
                        img.Save(fileName);
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
            return false;
        }
    }
}
